<?php

namespace Drupal\shushengyuan\Schemas;

use Drupal\shusy_point_product\Entity\ShusyPointProductOrder;
use stdClass;

/**
 * @OA\Schema(
 *   schema="PointExchangeOrder",
 *   type="object",
 *   description="PointExchangeOrder"
 * )
 */
class PointExchangeOrder extends AbstractBaseModel {

  protected function getEntityType() {
    return 'shusy_point_product_order';
  }

  protected function setValue() {
    if ($this->entity instanceof ShusyPointProductOrder) {
      $value = [
        /**
         * @OA\Property(
         *   property="id",
         *   type="integer",
         *   description="ID",
         * ),
         */
        'id' => (int) $this->entity->id,
        /**
         * @OA\Property(
         *   property="address",
         *   type="object",
         *   description="选择的收货地址 如果没有选择地址返回 false",
         *   ref="#/components/schemas/AddressItem",
         * ),
         */
        'address' => $this->getAddress($this->entity),
        /**
         * @OA\Property(
         *   property="point",
         *   type="integer",
         *   description="所需积分",
         * ),
         */
        'point' => $this->entity->point,
        /**
         * @OA\Property(
         *   property="amount",
         *   type="number",
         *   description="需支付价格",
         * ),
         */
        'amount' => $this->convertYuan($this->entity->amount),
        /**
         * @OA\Property(
         *   property="product",
         *   type="object",
         *   description="兑换的产品",
         *   ref="#/components/schemas/PointProductItem",
         * ),
         */
        'product' => PointProductItem::get($this->entity->point_product_id),
        /**
         * @OA\Property(
         *   property="validity",
         *   type="integer",
         *   description="支付有效期剩余秒数 待支付有效 其它状态忽略",
         * ),
         */
        'validity' => $this->getValidity($this->entity),
      ];
      $this->merge($value);
    }
  }

  protected function getAddress(ShusyPointProductOrder $order) {
    if (!empty($order->username) && !empty($order->telnumber)) {
      $tmp = new stdClass();
      $tmp->userName = $order->username;
      $tmp->telNumber = $order->telnumber;
      $tmp->nationalCode = $order->nationalcode;
      $tmp->provinceName = $order->provincename;
      $tmp->cityName = $order->cityname;
      $tmp->countyName = $order->countyname;
      $tmp->detailInfo = $order->detailinfo;
      return $tmp;
    }
    return FALSE;
  }

  protected function getValidity(ShusyPointProductOrder $order) {
    $t = $order->validity - REQUEST_TIME;
    return ($t > 0) ? $t : 0;
  }
}
